package com.lbs.tdrive.travel;


import java.util.List;

import com.lbs.tdrive.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lbs.tdrive.adapter.AlertAdapter;
import com.lbs.tdrive.adapter.VehicleAdapter;
import com.lbs.tdrive.base.TDriveBaseActivity;
import com.lbs.tdrive.comon.HttpASync;
import com.lbs.tdrive.comon.Utils;
import com.lbs.tdrive.comon.HttpASync.OnRequest;
import com.lbs.tdrive.constant.AppConstant;
import com.lbs.tdrive.data.AlertData;
import com.lbs.tdrive.data.Vehicle;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AlertActivity extends TDriveBaseActivity implements OnClickListener, OnRequest ,OnItemClickListener{
	private ListView alertList;
	private AlertAdapter adapter;
	private TextView alerttext;
	private ImageView deletealert;
	private Context ctx;
	public static final int DELETE_ALERT=1;
	private List<AlertData> alerteListData ;
	private Handler handler=new Handler(){
		

		public void handleMessage(android.os.Message msg) {
			if(msg.what==AlertActivity.DELETE_ALERT) {
				
				Toast.makeText(ctx, "Delete", Toast.LENGTH_SHORT).show();
				
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ctx=this;
		setContentView(R.layout.alert);
		//alerttext=(TextView)findViewById(R.id.alerttext);
		//deletealert=(ImageView)findViewById(R.id.deletealert);
		
		
		alertList=(ListView) findViewById(R.id.alertlist);
		alertList.setOnItemClickListener(this);
		
		//alerttext.setOnClickListener(this);
		//deletealert.setOnClickListener(this);
	
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onResponseReceived(String response) {
		Gson gson = new Gson();
		
		 alerteListData = gson.fromJson(response,new TypeToken<List<AlertData>>(){}.getType());
		for (AlertData alert : alerteListData) {
			System.out.println("Alert : "+alert.getAlertName());
		}
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				adapter=new AlertAdapter(ctx,handler,alerteListData);
				alertList.setAdapter(adapter);
				hideProgress();
			}
		});
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
		String uName = pref.getString("username", null);
		HttpASync async = new HttpASync();
		String url=AppConstant.ALERT+"?username="+uName+"&lat=8.521056&long=76.941712";
		async.setOnRequestListener(this);
		async.execute(url);
		showProgress("Retriving Alerts..");
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
	
		AlertData alert  = alerteListData.get(position);
		String place = Utils.getAddress(ctx, alert.getLatitude(), alert.getLongitude());
		String des = alert.getAlertName() + " at " + place;
		Toast.makeText(ctx, des, Toast.LENGTH_LONG).show();
		
	}
	
}
